package com.atguigu.gmall.list.receiver;


import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.list.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ClassName: ListReceiver
 * Package: com.atguigu.gmall.list.receiver
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-09-04 19:42
 * @Version 1.0
 */
@Component
public class ListReceiver {
 @Autowired
 private SearchService searchService;

 /**
  * 监听消息 实现商品上架
  * @param skuId
  * @param message
  * @param channel
  */
 @SneakyThrows
 @RabbitListener(bindings = @QueueBinding(
         value = @Queue(value = MqConst.QUEUE_GOODS_UPPER,durable = "true",autoDelete = "false"),
         exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
         key = {MqConst.ROUTING_GOODS_UPPER}
 ))
 public void goodsUpper(Long skuId, Message message , Channel channel){
  try {
   //  判断
   if (null != skuId){
    searchService.upperGoods(skuId);
   }
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
  //  手动签收
  channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
 }

 /**
  * 监听消息 实现商品下架
  * @param skuId
  * @param message
  * @param channel
  */
 @SneakyThrows
 @RabbitListener(bindings = @QueueBinding(
         value = @Queue(value = MqConst.QUEUE_GOODS_LOWER,durable = "true",autoDelete = "false"),
         exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_GOODS),
         key = {MqConst.ROUTING_GOODS_LOWER}
 ))
 public void goodsLower(Long skuId, Message message , Channel channel){
  try {
   //  判断
   if (null != skuId){
    searchService.lowerGoods(skuId);
   }
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
  //  手动签收
  channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
 }
}



